package com.itheima.controller;


import com.itheima.common.entity.Result;
import com.itheima.common.utils.SMSUtils;
import com.itheima.common.utils.ValidateCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/validatecode")
@Slf4j
@Api(tags = "体检预约发送验证码")
public class ValidatecodeController {
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/send4Order")
    @ApiOperation(value = "体检预约发送验证码")
    public Result send4Order(String telephone) {
        log.info("发送验证码：{}", telephone);
        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        try {
            SMSUtils.sendMessage(SMSUtils.ORDER_NOTICE, telephone, code);
            //验证码保存到redis，保存5分钟，超时自动删除
            redisTemplate.opsForValue().set(telephone, code, 665, TimeUnit.MINUTES);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "发送验证码失败");
        }
        return new Result(true, "发送验证码成功");
    }
}
